home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / HexEdit 1.21 / ~Project / Source / EditScrollBar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-03  |  5.5 KB  |  213 lines  |  [TEXT/CWIE]

  1. /*********************************************************************
  2.  * EditScrollBar.c
  3.  *
  4.  * HexEdit, a simple hex editor
  5.  * copyright 1993, Jim Bumgardner
  6.  *********************************************************************/
  7. #include "HexEdit.h"
  8.  
  9. ControlActionUPP trackActionUPP;        //LR :init'd in Main!
  10.  
  11. void SetupScrollBars(EditWindowPtr dWin)
  12. {
  13.     Rect    sRect,r;
  14.     // !! Set up vertical scroll bar
  15.     //
  16.     r = ((WindowPtr) dWin)->portRect;
  17.     sRect.left = r.right - (SBarSize - 1);
  18.     sRect.top = r.top - 1;
  19.     sRect.right = r.right + 1;
  20.     sRect.bottom = r.bottom  - GrowIconSize;
  21.      dWin->vScrollBar = NewControl((WindowPtr) dWin,&sRect,"\p",true,
  22.                                  0,0,sRect.bottom - sRect.top,scrollBarProc,1L);
  23.     AdjustScrollBars((WindowPtr) dWin, 1);
  24. }
  25.  
  26. //
  27. // Adjust scroll bars when they need to be redrawn for some reason.
  28. //
  29. // resizeFlag is an optimization to avoid extra work when you aren't
  30. // resizing.
  31. //
  32.  
  33. void AdjustScrollBars(WindowPtr theWin, short resizeFlag)
  34. {
  35.     short            w,h;
  36.     GrafPtr            savePort;
  37.     EditWindowPtr    dWin = (EditWindowPtr) theWin;
  38.     long            limit;
  39.  
  40.     GetPort(&savePort);
  41.  
  42.     if (resizeFlag) {
  43.         // Adjust Lines Per Page
  44.         dWin->linesPerPage = ((theWin->portRect.bottom - SBarSize) - HeaderHeight - TopMargin - BotMargin)/LineHeight;
  45.  
  46.         // Move sliders to new position
  47.         //
  48.         MoveControl(dWin->vScrollBar, theWin->portRect.right - (SBarSize - 1),
  49.                                             theWin->portRect.top - 1);    
  50.         // Change their sizes to fit new window dimensions
  51.         //
  52.         w = 16;
  53.         h = theWin->portRect.bottom - theWin->portRect.top - (GrowIconSize - 1);
  54.         SizeControl(dWin->vScrollBar,w,h);
  55.  
  56.         // Reset their maximum values
  57.         //
  58.         SetControlMaximum(dWin->vScrollBar,h);
  59.     }
  60.  
  61.     // Reposition painting if you have resized or scrolled past the legal
  62.     // bounds  Note: this call will usually be followed by an update
  63.     //
  64.     limit = ((dWin->fileSize+15) & 0xFFFFFFF0) - (dWin->linesPerPage << 4);
  65.     if (dWin->editOffset > limit)
  66.         dWin->editOffset = limit;
  67.     if (dWin->editOffset < 0)
  68.         dWin->editOffset = 0;
  69.  
  70.     // Set the value of the sliders accordingly
  71.     //
  72.     h = theWin->portRect.bottom - theWin->portRect.top - (GrowIconSize - 1);
  73.     if (limit > 0) {
  74.         h = theWin->portRect.bottom - theWin->portRect.top - (GrowIconSize - 1);
  75.         SetControlMaximum(dWin->vScrollBar,h);
  76.         if (dWin->editOffset < 64000L)
  77.             SetControlValue(dWin->vScrollBar,(short) ((dWin->editOffset*h)/limit));
  78.         else
  79.             SetControlValue(dWin->vScrollBar,(short) (dWin->editOffset / (limit/h)));
  80.     }
  81.     else {
  82.         SetControlMaximum(dWin->vScrollBar,0);
  83.         SetControlValue(dWin->vScrollBar,0);
  84.     }
  85.         
  86.     SetPort(savePort);
  87. }
  88.  
  89. //
  90. // Callback routine to handle arrows and page up, page down
  91. //
  92. EditWindowPtr    gDWin;
  93.  
  94. pascal void MyScrollAction(ControlHandle theControl, short thePart)
  95. {
  96. #pragma unused (theControl)    //LR :fix warnings
  97.  
  98.     long        curPos,newPos;
  99.     short        pageWidth;
  100.     Rect        myRect;
  101.     WindowPtr    gp = (WindowPtr) gDWin;
  102.  
  103.     curPos = gDWin->editOffset;
  104.     newPos = curPos;
  105.  
  106.     myRect = ((WindowPtr) gDWin)->portRect;
  107.     myRect.right -= SBarSize-1;
  108.     myRect.bottom -= SBarSize-1;
  109.  
  110.     pageWidth = (gDWin->linesPerPage-1)*16;
  111.  
  112.     switch (thePart) {
  113.       case kControlUpButtonPart:        newPos = curPos - 16;            break;
  114.       case kControlDownButtonPart:    newPos = curPos + 16;            break;
  115.       case kControlPageUpPart:            newPos = curPos - pageWidth;    break;
  116.       case kControlPageDownPart:        newPos = curPos + pageWidth;    break;
  117.     }
  118.  
  119.     if (newPos != curPos) {
  120.         HEditScrollToPosition(gDWin, newPos);
  121.     }
  122. }
  123.  
  124. // Intercept Handler for scroll bars
  125. // Returns true if user clicked on scroll bar
  126. //
  127.  
  128. Boolean HandleControlClick(WindowPtr theWin, Point where)
  129. {
  130.     short             ctlPart;
  131.     ControlHandle    whichControl;
  132.     short            vPos;
  133.     EditWindowPtr    dWin = (EditWindowPtr) theWin;
  134.     ControlActionUPP trackActionProc;
  135.  
  136.     // Check if user clicked on scrollbar
  137.     //
  138.     if ((ctlPart = FindControl(where,theWin,&whichControl)) == 0)
  139.         return false;
  140.  
  141.     // Identify window for callback procedure
  142.     //
  143.     gDWin = dWin;
  144.  
  145.     // Use default behavior for thumb
  146.     // Program will crash if you don't!!
  147.     //
  148.     if (ctlPart == kControlIndicatorPart)
  149.         trackActionProc = 0L;
  150.     else
  151.         trackActionProc = trackActionUPP;    //LR :Universal Headers requirement fix
  152.  
  153.     // Perform scrollbar tracking
  154.     //
  155.     if ((ctlPart = TrackControl(whichControl, where, trackActionProc)) == 0)    //LR :see above
  156.         return false;
  157.  
  158.     if (ctlPart == kControlIndicatorPart) {
  159.         long    newPos,h,limit;
  160.         vPos = GetControlValue(dWin->vScrollBar);
  161.         h = theWin->portRect.bottom - theWin->portRect.top - (GrowIconSize - 1);
  162.         limit = ((dWin->fileSize+15) & 0xFFFFFFF0) - (dWin->linesPerPage << 4);
  163.         if (vPos == h)
  164.             newPos = ((dWin->fileSize+15) & 0xFFFFFFF0) - (dWin->linesPerPage << 4);
  165.         else if (limit < 64000L)        // JAB 12/10 Prevent Overflow in Calcuation
  166.             newPos = (vPos*limit)/h;
  167.         else
  168.             newPos = vPos*(limit/h);
  169.         newPos -= newPos & 0x0F;
  170.         HEditScrollToPosition(dWin, newPos);
  171.     }
  172.  
  173.     return true;
  174. }
  175.  
  176. // Scroll to an explicit position
  177. //
  178. void HEditScrollToPosition(EditWindowPtr dWin, long newPos)
  179. {
  180.     long    limit;
  181.  
  182.     SetPort((WindowPtr) dWin);
  183.  
  184.     // Constrain scrolling position to legal limits
  185.     //
  186.     limit = ((dWin->fileSize+15) & 0xFFFFFFF0) - (dWin->linesPerPage << 4);
  187.     if (newPos > limit)
  188.         newPos = limit;
  189.     if (newPos < 0)
  190.         newPos = 0;
  191.     dWin->editOffset = newPos;
  192.     
  193.     // Adjust Scrollbars
  194.     AdjustScrollBars((WindowPtr) dWin, false);
  195.  
  196.     // 12/10/93 - Optimize Drawing
  197.     SetCurrentChunk(dWin, dWin->editOffset);
  198.  
  199.     DrawPage(dWin);
  200.     UpdateOnscreen((WindowPtr) dWin);
  201. }
  202.  
  203. void AutoScroll(EditWindowPtr dWin, Point pos)
  204. {
  205.     short    offset;
  206.     if (pos.v < HeaderHeight+TopMargin)
  207.         offset = -16;
  208.     else if (pos.v >= HeaderHeight+TopMargin+dWin->linesPerPage*LineHeight)
  209.         offset = 16;
  210.     else
  211.         return;
  212.     HEditScrollToPosition(dWin, dWin->editOffset+offset);
  213. }